//
//  GYScrollerView.m
//  GoodLuck
//
//  Created by apple on 2018/1/31.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYScrollerView.h"

//滑动式图中的UIImageView的个数
static const int imageViewCount = 3;

@interface GYScrollerView ()<UIScrollViewDelegate>

/** 滑动式图 */
@property (nonatomic, strong) UIScrollView *scrollView;

/** 储存图片显示器的数组 */
@property (nonatomic, strong) NSMutableArray *imageViews;

/** 定时器 */
@property (nonatomic, strong) NSTimer *timer;

/** 记录当前正在显示的图片下标 */
@property (nonatomic, assign) NSInteger currentIndex;

@property (nonatomic, getter=isManual) BOOL manual;
@end
@implementation GYScrollerView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        //初始化
        [self setupScrollerView];
    }
    return self;
}

- (void)setupScrollerView {
    self.imageViews = [NSMutableArray array];
    //1.添加UIscrollView
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    scrollView.contentSize = CGSizeMake(ScreenWidth*3, self.height);
    scrollView.bounces = NO;
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    scrollView.contentOffset  = CGPointMake(ScreenWidth, 0);
    
    [self addSubview:scrollView];
    self.scrollView = scrollView;
    
    //1.给scrollView添加一个点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(topGR:)];
    [scrollView addGestureRecognizer:tap];
    
    //添加图片空间
    for (int i = 0; i < imageViewCount; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*ScreenWidth, 0, ScreenWidth, self.height)];
        imageView.tag = i;
        [self.imageViews addObject:imageView];
        [scrollView addSubview:imageView];
    }
    self.currentIndex = 0;
}
- (void)setImages:(NSArray *)images {
    _images = images;

    //复制图片
    for (int i = 0; i < self.imageViews.count; i++) {
        UIImageView *imageView = self.imageViews[i];
        switch (i) {
            case 0:
            {
                imageView.image = images[images.count-1];//[UIImage imageNamed:images[images.count-1]];
            }
                break;
                
            case 1:
            {
                imageView.image = [images firstObject];//[UIImage imageNamed:[images firstObject]];
            }
                break;
                
            case 2:
            {
                if (images.count > 1) {
                    imageView.image = images[1];//[UIImage imageNamed:images[1]];
                }else {
                    imageView.image = [images firstObject];//[UIImage imageNamed:[images firstObject]];
                }
            }
                break;
                
            default:
                break;
        }
    }
    //添加定时器
    //[self startTimer];
}

- (void)topGR:(UITapGestureRecognizer *)tap {
    [self.delegate clickScrollView:self.currentIndex];
}

- (void)startTimer {
    self.manual = YES;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(changedOffset) userInfo:nil repeats:YES];
}

- (void)stopTimer {
    self.manual = NO;
    [self.timer invalidate];
    self.timer = nil;
}

- (void)changedOffset {
    //自动滑动
//    if (self.currentIndex == self.images.count) {
//        self.currentIndex = 0;
//    }
    //self.scrollView.contentOffset = CGPointMake(self.currentIndex*ScreenWidth, 0);
    [self.scrollView setContentOffset:CGPointMake(3*ScreenWidth, 0) animated:YES];
    //self.currentIndex += 1;
    [UIView animateWithDuration:1.0 animations:^{
//        self.scrollView.contentOffset = CGPointMake(3*ScreenWidth, 0);
    }];
}

#pragma mark -- UIScrollerViewDelegate
//修改contentoffset属性  并且带有动画的时候 会执行这个方法  没有动画则不会执行这个动画
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    NSLog(@"什么时候调用啊.....");
    //    //1.1 更换image的图片
    //    if (self.images.count > 1 && self.currentIndex < self.images.count-1) {
    //        self.currentIndex += 1;
    //    }else {
    //        self.currentIndex = 0;
    //    }
    for (int i = 0; i < self.imageViews.count; i++) {
        
        UIImageView *imageView = self.imageViews[i];
        switch (i) {
                //            case 0:
                //            {
                //                if (self.images.count > 1) {
                //                    if (self.currentIndex > 0) {
                //                        imageView.image = [UIImage imageNamed:self.images[self.currentIndex-1]];
                //                    }else if (self.currentIndex == self.images.count-1) {
                //                        imageView.image = [UIImage imageNamed:self.images[self.images.count-1]];
                //                    }
                //                }else {
                //                    imageView.image = [UIImage imageNamed:self.images[0]];
                //                }
                //            }
                //                break;
                
            case 1:
            {
                imageView.image = self.images[self.currentIndex];//[UIImage imageNamed:self.images[self.currentIndex]];
            }
                break;
                
            case 2:
            {
                if (self.images.count > 1 && self.currentIndex < self.images.count-1) {
                    imageView.image = self.images[self.currentIndex+1];//[UIImage imageNamed:self.images[self.currentIndex+1]];
                }else {
                    imageView.image = self.images[0];//[UIImage imageNamed:self.images[0]];
                }
            }
                break;
        }
    }
    //1.1 更换image的图片
    if (self.images.count > 1 && self.currentIndex < self.images.count-1) {
        self.currentIndex += 1;
    }else {
        self.currentIndex = 0;
    }
    
    //2.修改contentOffSet属性
    [scrollView setContentOffset:CGPointMake(ScreenWidth, 0)];
}

//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
//    
//    [self stopTimer];
//}
//
//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
//    [self startTimer];
//}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //if (!self.isManual) {
        //滑动结束应该要调用刷新图片
        //1.首先应该判断偏移量是不是超过一半图片
        CGFloat offsetX = scrollView.contentOffset.x;
        if (offsetX >= ScreenWidth + ScreenWidth/2) {
            //1.1 更换image的图片
            if (self.images.count > 1 && self.currentIndex < self.images.count-1) {
                self.currentIndex += 1;
            }else {
                self.currentIndex = 0;
            }
            for (int i = 0; i < self.imageViews.count; i++) {
                
                UIImageView *imageView = self.imageViews[i];
                switch (i) {
                    case 0:
                    {
                        if (self.images.count > 1) {
                            if (self.currentIndex > 0) {
                                imageView.image = self.images[self.currentIndex-1];//[UIImage imageNamed:self.images[self.currentIndex-1]];
                            }else if (self.currentIndex == self.images.count-1) {
                                imageView.image = self.images[self.images.count-1];//[UIImage imageNamed:self.images[self.images.count-1]];
                            }
                        }else {
                            imageView.image = self.images[0];//[UIImage imageNamed:self.images[0]];
                        }
                    }
                        break;
                        
                    case 1:
                    {
                        imageView.image = self.images[self.currentIndex];//[UIImage imageNamed:self.images[self.currentIndex]];
                    }
                        break;
                        
                    case 2:
                    {
                        if (self.images.count > 1 && self.currentIndex < self.images.count-1) {
                            imageView.image = self.images[self.currentIndex+1];//[UIImage imageNamed:self.images[self.currentIndex+1]];
                        }else {
                            imageView.image = self.images[0];//[UIImage imageNamed:self.images[0]];
                        }
                    }
                        break;
                }
            }
            //2.修改contentOffSet属性
            [scrollView setContentOffset:CGPointMake(ScreenWidth, 0)];
            
        }else if(offsetX <= ScreenWidth/2) {
            if (self.images.count > 1 && self.currentIndex > 0) {
                self.currentIndex -= 1;
            }else {
                self.currentIndex = self.images.count-1;
            }
            for (int i = 0; i < self.imageViews.count; i++) {
                UIImageView *imageView = self.imageViews[i];
                switch (i) {
                    case 0:
                    {
                        if (self.images.count > 1) {
                            if (self.currentIndex == 0) {
                                imageView.image = self.images[self.images.count-1];//[UIImage imageNamed:self.images[self.images.count-1]];//最后一张图片
                            }else if (self.currentIndex > 0) {
                                imageView.image = self.images[self.currentIndex-1];//[UIImage imageNamed:self.images[self.currentIndex-1]];
                            }
                        }else {
                            imageView.image = self.images[0];//[UIImage imageNamed:self.images[0]];
                        }
                    }
                        break;
                        
                    case 1:
                    {
                        imageView.image = self.images[self.currentIndex];//[UIImage imageNamed:self.images[self.currentIndex]];
                    }
                        break;
                        
                    case 2:
                    {
                        if (self.images.count > 1) {
                            if (self.currentIndex < self.images.count -1) {
                                imageView.image = self.images[self.currentIndex+1];//[UIImage imageNamed:self.images[self.currentIndex+1]];
                            }else if (self.currentIndex == self.images.count -1) {
                                imageView.image = self.images[0];//[UIImage imageNamed:self.images[0]];
                            }
                        }else {
                            imageView.image = self.images[0];//[UIImage imageNamed:self.images[0]];
                        }
                    }
                        break;
                }
            }
            [scrollView setContentOffset:CGPointMake(ScreenWidth, 0)];
        }
//    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
